package sql_sqlx

import (
	"clean/domain"

	"github.com/jmoiron/sqlx"
)

type HelloRepo struct {
	db *sqlx.DB
}

func NewHelloRepo(db *sqlx.DB) *HelloRepo {
	return &HelloRepo{
		db: db,
	}
}

type HelloDao struct {
	Name string `db:"name"`
	Msg  string `db:"msg"`
}

func (r *HelloRepo) GetByName(name string) (*domain.Hello, error) {
	user := &HelloDao{}
	err := r.db.Get(user, "select * from hello where name=?", name)
	if err != nil {
		return nil, err
	}
	return &domain.Hello{
		Name: user.Name,
		Msg:  user.Msg,
	}, nil
}

func (r *HelloRepo) Save(hello *domain.Hello) {
	helloDao := &HelloDao{
		Name: hello.Name,
		Msg:  hello.Msg,
	}
	r.db.NamedExec("insert into hello (name, msg) values (:name, :msg)", helloDao)
}
